package com.ruoyi.dida.db.manager.controller;

import com.ruoyi.dida.db.manager.entity.DBInfo;
import com.ruoyi.dida.db.manager.service.DBService;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "数据仓库接口")
@Controller
@RequestMapping("/db/info")
public class DBController extends BaseController {
    private String prefix = "db/info";
    @Autowired
    private DBService dbService;

    @ApiOperation("数据库列表页面")
    @RequiresPermissions("db:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    @PostMapping("/list")
    @ApiOperation("数据库列表")
    @RequiresPermissions("db:info:list")
    @ResponseBody
    public TableDataInfo list(){
        startPage();
        List<DBInfo> list = dbService.list();
        return getDataTable(list);
    }

    /**
     * 新增数据库操作
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }
    // 传递的参数一定要用JSON.stringify(paramdata); 方法将参数转换成json格式的字符串; 因为SpringBoot 中@RequestBody注解要求的必须是json格式的字符串才能注入参数
    // headers : {"Content-Type" : "application/json;charset=utf-8"},
    @PostMapping("/add")
    @ApiOperation("新增数据库")
    @ResponseBody
    public AjaxResult addDB(DBInfo dbInfo){
        return toAjax(dbService.addDB(dbInfo));
    }


    /**
     * 修改数据库操作
     */
    @ApiOperation("修改数据库")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long dbId, ModelMap mmap) {
        DBInfo dBInfo = dbService.selectDbInfo(dbId);
        mmap.put("dbInfo", dBInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据库操作
     */
    @RequiresPermissions("db:info:edit")
    @Log(title = "数据库修改操作", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DBInfo dBInfo) {
        return toAjax(dbService.updateDbInfo(dBInfo));
    }

    /**
     * 根据数据库名-删除-数据库信息
     */
    @RequiresPermissions("db:info:remove")
    @Log(title = "根据数据库名-删除-数据库信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Long ids) {
        System.out.println("dbId=="+ids);
        return toAjax(dbService.delectDbInfoByDbId(ids));
    }



}
